$(document).ready(function(){
	var VAL = {
			T_FBPLAYOUT : "FBPBISLAY",
			NORMAL : "0000000",
			DISABLED_ATTR : {
				"disabled" : true
			},
			ENABLED_ATTR : {
				"disabled" : false
			}
		};
	var controlsQuery = {
			W   : $(window),
			
			$layotIDTxt : $("#layotIDTxt"),
			$layotDscTxt : $("#layotDscTxt"),
			$layotCateSel : $("#layotCateSel"),
			$xAxisCntTxt : $("#xAxisCntTxt"),
//			$xAxisLenTxt   : $("#xAxisLenTxt"),
			$yAxisCntTxt: $("#yAxisCntTxt"),
//			$yAxisLenTxt: $("#yAxisLenTxt"),
			
			buttons : {
				$query_btn 	: $("#query_btn"),
		        $delete_btn 	: $("#delete_btn"),
		        $add_btn     : $("#add_btn"),
		        $update_btn  : $("#update_btn"),
		        $copy_btn    : $("#copy_btn"),
		        $save_btn : $("#save_btn"),
		        $rollback_btn : $("#rollback_btn"),
		        $clear_btn : $("#clear_btn"),
				$addBomMtrl : $("#add_bom_btn"),
				$updateBomMtrl : $("#update_bom_btn"),
				$deleteBomMtrl : $("#del_bom_btn")
			},
			mainGrd   :{
	            grdId     : $("#layotListGrd")   ,
	            grdPgText : $("#layotListPg" )   ,
	            fatherDiv : $("#layotListDiv")
	        },
			dialogs : {
				$querylayotDialog : $("#querylayotDialog")
			}
		};

	
	/**
     * All button's jquery object
     * @type {Object}
     */
    var btnQuery = {
        query_btn : $("#query_btn"),
        delete_btn : $("#delete_btn"),
        add_btn : $("#add_btn"),
        save_btn : $("#save_btn"),
        update_btn : $("#update_btn"),
        copy_btn : $("#copy_btn"),
        rollback_btn : $("#rollback_btn"),
        clear_btn : $("#clear_btn"),
        loc_btn:$("#location_btn")
    };
	
	var toolFunc = {
			resizeFnc : function(){ 
		    	comResize(controlsQuery.W,controlsQuery.mainGrd.fatherDiv,controlsQuery.mainGrd.grdId);
		    },
		    initFnc : function(){
		        $("input").val("");
		        $("select").empty();
		        $("#layotConditionForm input").attr(VAL.DISABLED_ATTR);
				$("#layotConditionForm select").attr(VAL.DISABLED_ATTR);
		    },
		    iniLYCTSelect: function(){
		    	comAddValueByDataCateFnc("#layotCateSel","LYCT","data_ext","data_desc",false);
		    },
		    com_get_1st_inf : function(){
				var rowIds = controlsQuery.mainGrd.grdId.jqGrid('getDataIDs');
				if(rowIds.length>0){
					controlsQuery.mainGrd.grdId.setSelection(rowIds[0], true);
//					controlsFunc.mdlGridSelRowFnc(rowIds[0]);
				}
			},
			layotGridSelRowFnc : function(rowId) {
				if(controlsQuery.$layotDscTxt.attr("disabled") != "disabled"){
					showErrorDialog("", "目前处于编辑中，请勿进行查询操作");
					return false;
				}
				var rowData, layotId, iary, inObj, outObj, oary, i, tblCnt, oaryArr;
				$("input").attr(VAL.DISABLED_ATTR);
				$("select").attr(VAL.DISABLED_ATTR);
				
				rowData = controlsQuery.mainGrd.grdId.jqGrid("getRowData", rowId);
				layotId = rowData.layot_id;
				toolFunc.setLocationGrid(layotId);
				inObj = {
					trx_id : VAL.T_FBPLAYOUT,
					action_flg : "Q",
					iary : [{
						layot_id : layotId
					}]
				};
				outObj = comTrxSubSendPostJson(inObj);
				if (outObj.rtn_code == VAL.NORMAL) {
					oary = outObj.oary[0];
					if (!oary) {
						return false;
					}

					controlsQuery.$layotIDTxt.val(oary.layot_id.trim());
					
					SelectDom.setSelect(controlsQuery.$layotCateSel, oary.layot_cate.trim());
					
					
					controlsQuery.$layotDscTxt.val(oary.layot_dsc.trim());
					controlsQuery.$xAxisCntTxt.val(oary.x_axis_cnt);
//					controlsQuery.$xAxisLenTxt.val(oary.x_axis.trim());
					controlsQuery.$yAxisCntTxt.val(oary.y_axis_cnt);
//					controlsQuery.$yAxisLenTxt.val(oary.y_axis.trim());
				}
				
			},
			setLocationGrid:function(layotId){
				$("#layoutDiv").empty();
				var inObj,outObj;
				inObj = {
						trx_id:VAL.T_FBPLAYOUT,
						action_flg:"Q",
						iary:[{
							layot_id:layotId
						}]
				};
				outObj = comTrxSubSendPostJson(inObj);
				if (outObj.rtn_code === VAL.NORMAL) {
					setGridInfo(outObj.oary[0].oaryC,"#locationListGrd");
					if (outObj.oary[0].oaryC) {
						btnFunc.iniLocGrid();
					}
				}
				
			}
	};
	var btnFunc = {
	    	query_func : function(){
	    		/*** 将Div实例化为modal窗体 ***/
	    	    function diaLogQueryFnc(){
	    	    	var iary = {};
	    	    	if($("#querylayotDialog_layotIDTxt").val()!=""){
	    	    		iary.layot_id  = $("#querylayotDialog_layotIDTxt").val() ;
	    	    	}
	    	    	
	    	  
	    	    	 var inTrxObj ={
	    	    			 trx_id     : VAL.T_FBPLAYOUT ,
	    	    			 action_flg : "Q"        ,
	    	    			 iary       : [iary]
	    	    	 }
	    	    	 
	    	    	 var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
	    	    	 if(  outTrxObj.rtn_code == VAL.NORMAL ) {
	    	    		 setGridInfo(outTrxObj.oary,"#layotListGrd");
	    	    		 $('#querylayotDialog').modal("hide");
	    	    	 }
	    	    }
	    	         
	    	    $('#querylayotDialog').modal({
	    	        backdrop:true,
	    	        keyboard:false,
	    	        show:false
	    	    });
	    	    $('#querylayotDialog').unbind('shown.bs.modal');
	    	    $("#querylayotDialog_queryBtn").unbind('click');

	    	    $('#querylayotDialog').modal("show");
	    	    $("#querylayotDialog_queryBtn").bind('click',diaLogQueryFnc);
	    	    $("#querylayotDialog_layotIDTxt").attr({'disabled':false});
	    	},
	    	delete_func : function(){
	    		var selectRowId = controlsQuery.mainGrd.grdId.jqGrid("getGridParam","selrow");
	    	    if(!selectRowId){
	    	    	showErrorDialog("001","请选择需要删除的版式！");
	    	    	return false;
	    	    } 
	    	    var rowData = controlsQuery.mainGrd.grdId.jqGrid("getRowData",selectRowId);
	    	    $("#f1_query_btn").showCallBackWarnningDialog({
	    	    	errMsg  : "是否删除版式:["+ rowData.layot_id  + "],请确认!!!!",
	    	    	callbackFn : function(data) {
	    	    		if(data.result==true){
	    	    			var iary = [{
	    	    				layot_id : rowData.layot_id
	    	    			}]
	    	    			var inTrxObj={
	    	    					trx_id     : VAL.T_FBPLAYOUT,
	    	    					action_flg : "D"  ,
	    	    					iary       : iary
	    	    			}
	    	    			var outTrxObj = comTrxSubSendPostJson(inTrxObj);
	    	    			if(outTrxObj.rtn_code === VAL.NORMAL){
	    	    				showSuccessDialog("版式信息删除成功！！！");
	    	    				$("#layotListGrd").jqGrid("delRowData",selectRowId);
	        	                $("#layotConditionForm input").val("");
	        	                SelectDom.setSelect($("select"), "", "");
	    	    			}
	    	    		}
	    	    	}
	    	    });
	    	},
	    	add_func : function(){
	    		if($("#layotIDTxt").attr("disabled") != "disabled"){
					showErrorDialog("", "已经在新增中，请勿重复新增");
					return false;
				}
//	    		clear();
	    		$("input[type='text']").val("");
	    		SelectDom.setSelect($("select"), "", "");
	    		iniContorlData;
	    		$("#layotConditionForm input").attr(VAL.ENABLED_ATTR);
				$("#layotConditionForm select").attr(VAL.ENABLED_ATTR);
				$("#layoutGrd").jqGrid("clearGridData");
				$("#locationListGrd").jqGrid("clearGridData");
	    		$("#layotIDTxt").focus();
	    		
	    	},
	    	save_func : function(){
	    		var actionFlg , inObj, outObj, iary, newRowId;
	    		var layotFlg = 'S';
	    		var layotID,layotDsc,layotCateSel,xAxisCntTxt,yAxisCntTxt;
	    		actionFlg = $("#layotIDTxt").attr("disabled") === "disabled"
					? "U"
					: "A";
	    		layotID = $("#layotIDTxt").val().trim();
	    		layotDsc = $("#layotDscTxt").val().trim();
	    		layotCateSel = $("#layotCateSel").val().trim();
	    		xAxisCntTxt = $("#xAxisCntTxt").val().trim();
//	    		xAxisLenTxt = $("#xAxisLenTxt").val().trim();
	    		yAxisCntTxt = $("#yAxisCntTxt").val().trim();
//	    		yAxisLenTxt = $("#yAxisLenTxt").val().trim();
	    		
	    		if (!layotID) {
					showErrorDialog("", "版式名称不能为空");
					return false;
				}
	    		if (!layotDsc) {
					showErrorDialog("", "版式描述不能为空");
					return false;
				}
	    		if (!layotCateSel) {
					showErrorDialog("", "版式规格不能为空");
					return false;
				}
	    		if (!xAxisCntTxt) {
					showErrorDialog("", "X轴数量不能为空");
					return false;
				}
//	    		if (!xAxisLenTxt) {
//					showErrorDialog("", "X轴长度不能为空");
//					return false;
//				}
	    		if (!yAxisCntTxt) {
					showErrorDialog("", "Y轴数量不能为空");
					return false;
				}
//	    		if (!yAxisLenTxt) {
//					showErrorDialog("", "Y轴长度不能为空");
//					return false;
//				}

	    		//保存layot_lc
                var mrr_loc,iaryB,iaryC = [];
                var x_axis = controlsQuery.$xAxisCntTxt.val().trim();
                var rows = $("#layoutGrd").jqGrid('getRowData');
                var layot_id_fk = controlsQuery.$layotIDTxt.val();
                for(var i=0;i<rows.length;i++){
                    for(var j = 1; j< parseInt(x_axis) + 1 ; j++) {
                        mrr_loc = $("#layoutGrd").jqGrid('getCell',i+1,j*2);
                        if(!mrr_loc){
                        	showErrorDialog("","请填写完整！");
							return;
						}
                        iaryB = {
                                layot_id_fk: layot_id_fk,
                                x_axis: i + 1,
                                y_axis: j,
								pos_id: rows[i]["pos_id_"+j],
							 	mrr_loc: mrr_loc
                   			 }
                   			 iaryC.push(iaryB);
                        }
                }
	    		if (iaryC.length >0) {
					var inTrxObj = {
							trx_id : VAL.T_FBPLAYOUT,
							action_flg: 'C',
							iary : iaryC,
							evt_usr: 'C1602020'
					};
					var outTrxObj = comTrxSubSendPostJson(inTrxObj);
				}
	    		
	    		iary = {
	    			layot_id 	:  	layotID ,
	    			layot_dsc 	:  layotDsc,
	    			layot_cate : layotCateSel,
	    			x_axis_cnt :  xAxisCntTxt ,
//	    			x_axis  : xAxisLenTxt ,
	    			y_axis_cnt   :yAxisCntTxt,
	    			layot_flg	: layotFlg
//	    			y_axis   :yAxisLenTxt 
	    		}
	    		inObj = {
	    				trx_id : VAL.T_FBPLAYOUT,
	    				action_flg : actionFlg,
	    				evt_usr : "C1602020",
	    				iary : [iary]
	    			};
	    		var outObj = comTrxSubSendPostJson(inObj);
	    		if (outObj.rtn_code == VAL.NORMAL) {
					
					$("input").attr(VAL.DISABLED_ATTR);
					$("select").attr(VAL.DISABLED_ATTR);
					
					if (actionFlg == "A") {
						var newRowId = getGridNewRowID("#layotListGrd");
						controlsQuery.mainGrd.grdId.jqGrid("addRowData", newRowId,
								iary);
						controlsQuery.mainGrd.grdId.setSelection(newRowId, true); 
//						toolFunc.layotGridSelRowFnc(newRowId);
						showSuccessDialog("新增版式信息成功");
						
					} else if (actionFlg == "U") {
						var rowid = controlsQuery.mainGrd.grdId.jqGrid('getGridParam','selrow');
//						toolFunc.layotGridSelRowFnc(rowid);
						showSuccessDialog("版式信息更新成功");
					}
				}
	    		
	    	},
	    	update_func : function() {
				var selRowId = controlsQuery.mainGrd.grdId.jqGrid("getGridParam","selrow");
				if (!selRowId) {
					showErrorDialog("", "请选择需要修改的版式信息");
					return false;
				}
				$("#layotConditionForm input:not(#layotIDTxt,#layotVerTxt)")
						.attr(VAL.ENABLED_ATTR);
				$("#layotConditionForm select").attr(VAL.ENABLED_ATTR);
				controlsQuery.$layotDscTxt.focus();
			},
			copy_func:function(){
				var selRowId = controlsQuery.mainGrd.grdId.jqGrid("getGridParam","selrow");
				if (!selRowId) {
					showErrorDialog("", "请选择需要复制的版式信息");
					return false;
				}
				$("#layotConditionForm input").attr(VAL.ENABLED_ATTR);
				$("#layotConditionForm select").attr(VAL.ENABLED_ATTR);
				controlsQuery.$layotIDTxt.focus();
			},
			rollback_func : function(){
				var outObj,inObj_Query,layotId,iary ={};
				layotId = controlsQuery.$layotIDTxt.val();
				if(layotId){
					iary.layotId = layotId;
				}
				var inObj_Query = {
							trx_id : VAL.T_FBPLAYOUT,
							action_flg : 'Q',
							iary : [iary]
						};
				$("#layotConditionForm input").attr(VAL.DISABLED_ATTR);
				$("#layotConditionForm select").attr(VAL.DISABLED_ATTR);
				outObj = comTrxSubSendPostJson(inObj_Query);
				if (outObj.rtn_code === VAL.NORMAL) {
					setGridInfo(outObj.oary, "#layotListGrd");
					$("#queryOpeDialog").modal("hide");
					toolFunc.com_get_1st_inf();
				}
			},
			clear_func : function() {
				controlsQuery.mainGrd.grdId.jqGrid("clearGridData");
				$("input[type='text']").val("");
				SelectDom.setSelect($("select"), "", "");
                $("#layoutDiv").empty();
                $("input").attr(VAL.DISABLED_ATTR);
                $("select").attr(VAL.DISABLED_ATTR);

//				$("select").empty();
			},
			loc_func : function(){

	    		var infoCM=[];
	    		var x = controlsQuery.$xAxisCntTxt.val();
	    		var y = controlsQuery.$yAxisCntTxt.val();
	    		x_count = x;
	    		if( x == "" ){
	    			showErrorDialog("0003","X Count为空，请输入!");
					return false;
	    		}
	    		if( y == "" ){
	    			showErrorDialog("0003","Y Count为空，请输入!");
					return false;
	    		}
	            var num_34=[ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
	           				"A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
	        				"L", "M", "N", "P", "Q", "R", "S", "T", "U", "V",
	        				"W", "X", "Y", "Z" ];
	            var pos_35=[ "01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
	         				"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
	         				"21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
	         				"31", "32", "33", "34","35"];
	            for(var i=1;i<=x;i++){
	            	var str_pos={};
	            	var str_mrr={};
	            	str_pos = {name:"pos_id_"+i  ,index:"pos_id_"+i   ,label : i+" Location",width :"70px" ,align:"left" ,sortable:false,editable:true};
	            	str_mrr = {name:"mrr_loc_"+i ,index:"mrr_loc_"+i  ,label : i+" Mirror"  ,width :"70px" ,align:"left" ,sortable:false};
	            	infoCM.push(str_pos);
	            	infoCM.push(str_mrr);
	            }
	        	setLayoutGridInfo(infoCM);
	        	for(var j=1;j<=y;j++){
	        		var str="";
	        		for(var i=1;i<=x;i++){
	        			var mrr_loc_str = num_34[j-1] + num_34[i-1];//(x*(j-1)+i)
						var pos_id_str = pos_35[j-1] +"/"+ pos_35[i-1];
	        			str +="\"mrr_loc_"+i+"\":\""+mrr_loc_str+"\"" +",";
                        str +="\"pos_id_"+i+"\":\""+pos_id_str+"\"";
                        if( i != x ){
                            str += ",";
                        }
                    }
	        		console.log(str);
	        		var ss = "{" + str + "}";
	        		var rowData = eval("("+ss+")");
	        		$("#layoutGrd").jqGrid("addRowData", j, rowData);
	        	}
//	        	yDivChange("div-change");
			},
			iniLocGrid : function () {
	    		var infoCM=[];
	    		var layot_id_fk,x,y ;
	    		var selRowId = controlsQuery.mainGrd.grdId.jqGrid("getGridParam","selrow");
	    		if (selRowId) {
	    			var rowData = controlsQuery.mainGrd.grdId.jqGrid("getRowData",selRowId);
	    			layot_id_fk = rowData.layot_id;
				}
	    		var map ={};
	    		
	    		var inObj = {
	    				trx_id : VAL.T_FBPLAYOUT,
	    				action_flg : 'Q',
	    				iary : [{layot_id : layot_id_fk}]
	    		};
	    		var outObj = comTrxSubSendPostJson(inObj);
	    		if (outObj.rtn_code === VAL.NORMAL) {
	    			x = outObj.oary[0].x_axis_cnt;
	    			y = outObj.oary[0].y_axis_cnt;
	    			var oaryC = outObj.oary[0].oaryC;
	    			for (var i = 0; i < oaryC.length; i++) {
	    				map[oaryC[i].mrr_loc] = oaryC[i].pos_id;
	    			}
	    		}
	            var num_34=[ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
	           				"A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
	        				"L", "M", "N", "P", "Q", "R", "S", "T", "U", "V",
	        				"W", "X", "Y", "Z" ];
	            var pos_34=[ "A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
	         				"L", "M", "N", "P", "Q", "R", "S", "T", "U", "V",
	         				"W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", 
	         				"6", "7", "8", "9"];
	            for(var i=1;i<=x;i++){
	            	var str_pos={};
	            	var str_mrr={};
	            	str_pos = {name:"pos_id_"+i  ,index:"pos_id_"+i   ,label : i+" Location",width :"70px" ,align:"left" ,sortable:false,editable:true};
	            	str_mrr = {name:"mrr_loc_"+i ,index:"mrr_loc_"+i  ,label : i+" Mirror"  ,width :"70px" ,align:"left" ,sortable:false};
	            	infoCM.push(str_pos);
	            	infoCM.push(str_mrr);
	            }
	        	setLayoutGridInfo(infoCM);
	        	for(var j=1;j<=y;j++){
	        		var str="";
	        		for(var i=1;i<=x;i++){
	        			var mrr_loc_str = num_34[j-1] + num_34[i-1];//(x*(j-1)+i)
	        			if (mrr_loc_str in map) {
	        				str +="\"pos_id_"+i+"\":\""+map[mrr_loc_str]+"\"";
	        				if( i != (x+1) ){
	        					str += ",";
	        				}
	        			}
	        			str +="\"mrr_loc_"+i+"\":\""+mrr_loc_str+"\"";
	        			if( i != x ){
	        				str += ",";
	        			}
	        		}
	        		var ss = "{" + str + "}";
	        		var rowData = eval("("+ss+")");
	        		$("#layoutGrd").jqGrid("addRowData", j, rowData);
	        	}
//	        	yDivChange("div-change");
			}
	    };
	/**
     * grid  initialization
     */
    var iniGridInfo = function(){
        controlsQuery.mainGrd.grdId.jqGrid({
        	url:"",
       		datatype : "local",
    		mtype : "POST",
    		height : controlsQuery.mainGrd.fatherDiv.height(),
    		autowidth : true,//宽度根据父元素自适应
    		shrinkToFit : true,
    		scroll : false,
    		resizable : true,	
    		rownumbers : true,
    		loadonce : true,
    		viewrecords : true,
    		colModel : [{name: 'layot_id'       , index: 'layot_id'     , label: LAYOT_ID_TAG     , width: 100 }],
    		pager : controlsQuery.mainGrd.grdPgText,
    		onSelectRow : function(id) {
    			toolFunc.layotGridSelRowFnc(id);
			}
    	});
    };
    
    var setLayoutGridInfo = function(infoCM){
	    $("#layoutDiv").empty();
        var str = '<legend>Layout Location</legend><table id="layoutGrd"></table><div id="layoutPg"></div>';
    	$(str).appendTo($("#layoutDiv"));
		$("#layoutGrd").jqGrid({
            url:"",
            datatype:"local",
            mtype:"POST",
			height: ($(window).height() - $(".tab-content").offset().top - 290)*0.95-51-5,
            autowidth:true,
            shrinkToFit:false,
            scroll:true,
            rownumWidth : true,
            resizable : true,
            rowNum:40,
            loadonce:true,
            viewrecords:true,
            sortable:false,
            rownumbers  :true ,//显示行号
            rownumWidth : 30,  //行号列宽度
            pager : "#layoutPg",
            fixed: true,
            cellEdit:true,
            colModel: infoCM,
            cellsubmit:'clientArray',
            afterSaveCell : function(rowid,name,val,iRow,iCol) { 
            		/*var layot_id_fk = controlsQuery.$layotIDTxt.val();
            		var x_axis = name.split("_")[2];
            		if (!layot_id_fk) {
						showErrorDialog("","版式名称不能为空！");
						return false;
					}
            		var mrr_loc = $("#layoutGrd").jqGrid('getCell',rowid,iCol+1);
            		 var iaryB={
            				 layot_id_fk:layot_id_fk,
            				 mrr_loc:mrr_loc,
            				 x_axis : x_axis,
            				 y_axis	: parseInt(iRow),
            				 pos_id : val
            		 };
            		 iaryC.push(iaryB);*/
            	}
        });
	};
    
	var iniLocationGridInfo = function(){
		var infoCM = [
			{name:"x_axis"  ,index:"x_axis"   ,label : "X Axis"   ,width :"100px" ,align:"left" ,sortable:false},
			{name:"y_axis"  ,index:"y_axis"   ,label : "Y Axis"   ,width :"100px" ,align:"left" ,sortable:false},
			{name:"pos_id"  ,index:"pos_id"   ,label : "Location" ,width :"100px" ,align:"left" ,sortable:false},
			{name:"mrr_loc" ,index:"mrr_loc"  ,label : "Mirror"   ,width :"100px" ,align:"left" ,sortable:false}
	     ];
		$("#locationListGrd").jqGrid({
	        url:"",
	        datatype:"local",
	        mtype:"POST",
	        height:510,
	        autowidth:true,
	        shrinkToFit:false,
	        scroll:true,
	        rownumWidth : true,
	        resizable : true,
	        rowNum:40,
	        loadonce:true,
	        viewrecords:true,
	        sortable:false,
	        rownumbers  :true ,//显示行号
	        rownumWidth : 30,  //行号列宽度
	        pager : '#locationListPg',
	        fixed: true,
	        colModel: infoCM
	    });
	}
	
    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
        btnQuery.query_btn.click(function(){
            btnFunc.query_func();
        });
        btnQuery.delete_btn.click(function(){
            btnFunc.delete_func();
        });
        btnQuery.add_btn.click(function(){
            btnFunc.add_func();
        });
        btnQuery.save_btn.click(function(){
            btnFunc.save_func();
        });
        btnQuery.update_btn.click(function(){
            btnFunc.update_func();
        });
        btnQuery.copy_btn.click(function(){
            btnFunc.copy_func();
        });
        btnQuery.rollback_btn.click(function(){
            btnFunc.rollback_func();
        });
        btnQuery.clear_btn.click(function(){
            btnFunc.clear_func();
        });
        btnQuery.loc_btn.click(function(){
        	btnFunc.loc_func();
        });
    };
    
    /**
     * Ini contorl's data
     */
    var iniContorlData = function(){
    	toolFunc.initFnc();
        toolFunc.resizeFnc();
        toolFunc.iniLYCTSelect();
    };
	
    var clear = function(){
    	controlsQuery.mainGrd.grdId.jqGrid("clearGridData");
		$("input[type='text']").val("");
		$("select").empty();
    };
    
    var otherActionBind = function(){
        //Stop from auto commit
        $("form").submit(function(){
            return false;
        });
        
        controlsQuery.W.resize(function() {
        	toolFunc.resizeFnc();
    	});
        
    };
    
    /**
     * Ini view, data and action bind
     */
    var initializationFunc = function(){
    	iniGridInfo();
    	iniLocationGridInfo();
        iniButtonAction();
        otherActionBind();
        iniContorlData();
    };
    
    initializationFunc();


    //表格自适应
    function resizeFnc(){
        var offsetBottom, grdDivWidth,offsetBottom1, grdDivWidth1,offsetBottom2, grdDivWidth2;
        //1
        grdDivWidth = $("#table1Div").width();
        offsetBottom = $(window).height() - $("#table1Div").offset().top - 35;

        $("#layotListDiv").width(grdDivWidth*0.98);
        $("#layotListDiv").height(offsetBottom*0.99 - 20);
        $("#layotListGrd").setGridWidth(grdDivWidth*0.97);
        $("#layotListGrd").setGridHeight(offsetBottom*0.95-51);
        //2
        grdDivWidth1 = $(".tab-content").width();
        offsetBottom1 = $(window).height() - $(".tab-content").offset().top;

        $("#locationListDiv").width(grdDivWidth1*0.98);
        $("#locationListDiv").height(offsetBottom1*0.99 - 20);
        $("#locationListGrd").setGridWidth(grdDivWidth1*0.97);
        $("#locationListGrd").setGridHeight(offsetBottom1*0.95-51-5);

        //3
        grdDivWidth2 = $(".tab-content").width();
        offsetBottom2 = $(window).height() - $(".tab-content").offset().top - 290;

        $("#layoutDiv").width(grdDivWidth2*0.98);
        $("#layoutDiv").height(offsetBottom2*0.99 - 20);
        $("#layoutGrd").setGridWidth(grdDivWidth2*0.97);
        $("#layoutGrd").setGridHeight(offsetBottom2*0.95-51-5);
    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
});